#include "switchbutton.h"

SwitchButton::SwitchButton(QWidget *parent)
    : QWidget{parent}
{
    m_space = 2;
    m_radius = 5;
    m_checked = false;
    m_showText = true;
    m_animation = true;
    m_bgColorOn = QColor(21, 156, 119);
    m_bgColorOff = QColor(111, 122, 126);
    m_sliderColorOn = QColor(255, 255, 255);
    m_sliderColorOff = QColor(255, 255, 255);
    m_textColor = QColor(255, 255, 255);
    m_textOn = "开";
    m_textOff = "关";
    m_step = 4;
    m_startX = 0;
    m_endX = 0;
    m_timer = new QTimer(this);
    m_timer->setInterval(15);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}

void SwitchButton::setCheckedState(bool checked)
{
    m_checked = checked;

    if (m_checked) {
        m_endX = width() - height();
    } else {
        m_endX = 0;
    }

    if (m_animation) {
        m_timer->start();
    } else{
        m_startX = m_endX;
        update();
    }
}

void SwitchButton::paintEvent(QPaintEvent *ev)
{

    Q_UNUSED(ev)
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    drawBackground(&painter);
    drawSlider(&painter);
    drawText(&painter);
}

void SwitchButton::drawBackground(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);

    QColor bgColor = m_checked ? m_bgColorOn : m_bgColorOff;
    bgColor.setAlpha(140);

    painter->setBrush(bgColor);

    QRect rect(0, 0, width(), height());
    int side = qMin(width(), height());

    QPainterPath path1;
    path1.addEllipse(rect.x(), rect.y(), side, side);

    QPainterPath path2;
    path2.addEllipse(rect.width() - side, rect.y(), side, side);

    QPainterPath path3;
    path3.addRect(rect.x() + side / 2, rect.y(), rect.width() - side, height());

    QPainterPath path = path1 + path2 + path3;
    painter->drawPath(path);

    painter->restore();
}

void SwitchButton::drawText(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);

    int sliderWidth = qMin(height(), width()) - m_space * 2 - 5;
    if (m_checked){
        QRect textRect(0, 0, width() - sliderWidth, height());
        painter->setPen(QPen(m_textColor));
        painter->drawText(textRect, Qt::AlignCenter, m_textOn);
    } else {
        QRect textRect(sliderWidth, 0, width() - sliderWidth, height());
        painter->setPen(QPen(m_textColor));
        painter->drawText(textRect, Qt::AlignCenter, m_textOff);
    }

    painter->restore();
}

void SwitchButton::drawSlider(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);

    QColor color = m_checked ? m_sliderColorOn : m_sliderColorOff;

    painter->setBrush(QBrush(color));

    int sliderWidth = qMin(width(), height()) - m_space * 2;
    QRect rect(m_space + m_startX, m_space, sliderWidth, sliderWidth);
    painter->drawEllipse(rect);

    painter->restore();
}

void SwitchButton::updateValue()
{
    if (m_checked) {
        if (m_startX < m_endX) {
            m_startX += m_step;
        } else {
            m_startX = m_endX;
            m_timer->stop();
        }
    } else {
        if (m_startX > m_endX) {
            m_startX -= m_step;
        } else {
            m_startX = m_endX;
            m_timer->stop();
        }
    }
    update();
}

void SwitchButton::mousePressEvent(QMouseEvent *ev)
{
    Q_UNUSED(ev)

    m_checked = !m_checked;

    this->setCheckedState(m_checked);

    emit statusChanged(m_checked);
}
